package iso3.pt.action;

import java.util.Map;
import java.util.Set;

import iso3.pt.model.Asignatura;
import iso3.pt.model.Profesor;
import iso3.pt.service.PtDaoService;
import iso3.pt.service.PtService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StudentsManagementFormAction  extends ActionSupport 
{
	private static final long serialVersionUID = 1L;

	private String dni = null;
    private String password = null;
	private String nombre = null;
    private String telefono = null;

	private Profesor profesor = null;
	private Set<Asignatura> lecturerSubjects = null;
	
	public String execute() throws Exception 
	{
		PtService service = new PtDaoService();
		
		@SuppressWarnings("rawtypes")
		Map session = ActionContext.getContext().getSession();
		setProfesor((Profesor) session.get("profesor"));
		setLecturerSubjects((Set<Asignatura>) service.getAsignaturasProfesor(profesor.getId()));
		
		if (getDni().equals("") || getPassword().equals("") || getNombre().equals("") || getTelefono().equals("")) 
		{
			addActionError(getText("errors.invalid.studentManagement.specify"));
			return INPUT;
		}
		else
		{
			try
			{
				service.addAlumno(Integer.valueOf(getDni()), getPassword(), getNombre(), getTelefono());
				
				return SUCCESS;
			} 
			catch (NumberFormatException e)
			{
				addActionError(getText("errors.invalid.studentManagement"));
				
				return ERROR;
			}
		}
	}
	
	public Profesor getProfesor()
	{
		return profesor;
	}

	public void setProfesor(Profesor profesor)
	{
		this.profesor = profesor;
	}

	public Set<Asignatura> getLecturerSubjects()
	{
		return lecturerSubjects;
	}

	public void setLecturerSubjects(Set<Asignatura> lecturerSubjects)
	{
		this.lecturerSubjects = lecturerSubjects;
	}
	
	public String getDni()
	{
		return dni;
	}

	public void setDni(String dni)
	{
		this.dni = dni;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getNombre()
	{
		return nombre;
	}

	public void setNombre(String nombre)
	{
		this.nombre = nombre;
	}

	public String getTelefono()
	{
		return telefono;
	}

	public void setTelefono(String telefono)
	{
		this.telefono = telefono;
	}
}
